package org.achicu.db.rhino;

import org.achicu.db.StorableValueFactory;
import org.achicu.db.StorageException;
import org.achicu.db.values.StorableObject;
import org.achicu.db.values.StorableValue;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Wrapper;

public class RhinoStorableObject implements Scriptable, Wrapper {
	
	private StorableObject storableObject;
	
	public RhinoStorableObject(StorableObject object, Scriptable scope){
		this.storableObject = object;
		this.scope = scope;
	}	
	
	public StorableObject getStorableObject(){
		return storableObject;
	}
	
	
	public String getClassName() {
		return "StorableObject";
	}
	
	
	public void delete(String name) {
		try {
			storableObject.removeProperty(name);
		} catch (StorageException e) {
			e.printStackTrace();
		}
	}
	
	
	public void delete(int index) {
		System.out.println("Delete by index not implemented. Index = " + index);
	}
	
	
	public Object get(String name, Scriptable start) {
		if(name.equals("id")){
			return new Integer(storableObject.getId());
		}
		StorableValue result = null;
		try {
			result = storableObject.getProperty(name);
		} catch (StorageException e) {
			e.printStackTrace();
		}
		
		if(result==null)
			return null;
		
		return StorableValueFactory.unwrapValue(result, ScriptableObject.getTopLevelScope(this));		
	}
	
	public Object get(int index, Scriptable start) {
		return get(Integer.toString(index), start);
	}
	
	public Object getDefaultValue(Class hint) {
		// TODO Auto-generated method stub
		return storableObject.toString();
	}
	
	public Object[] getIds() {
		Object[] result = null;
		try {
			result = (Object[])storableObject.getProperties();
		} catch (StorageException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public Scriptable getParentScope() {
		return scope;
	}
	
	
	public Scriptable getPrototype() {
		return null;
	}
	
	
	public boolean has(String name, Scriptable start) {
		boolean result = false;
		try {
			result = storableObject.hasProperty(name);
		} catch (StorageException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	public boolean has(int index, Scriptable start) {
		return has(Integer.toString(index), start);
	}
	
	
	public boolean hasInstance(Scriptable instance) {
		return false;
	}
	
	
	public void put(String name, Scriptable start, Object value) {
		if(name.equals("id")){
			return;
		}
		StorableValue sValue = StorableValueFactory.wrapValue(value);
		if(sValue==null)
			return;
		
		try {
			storableObject.putProperty(name, sValue );
		} catch (StorageException e) {
			e.printStackTrace();
		}
	}
	
	
	public void put(int index, Scriptable start, Object value) {
		put(Integer.toString(index), start, value);
	}
	
	private Scriptable scope;
	
	
	public void setParentScope(Scriptable parent) {
		scope = parent;
	}
	
	
	public void setPrototype(Scriptable prototype) {
		;
	}

	
	public Object unwrap() {
		return storableObject;
	}

	
	
}
